/*
 ******************************************************************************
 *
 *       Copyright ASTRI, 2016.  All rights reserved.
 *
 *   This software is proprietary to and embodies the confidential technology
 *   of Hong Kong Applied Science and Technology Research Institute Company
 *   Limited (ASTRI).
 *   Possession, use, or copying of this software and media is authorized
 *   only pursuant to a valid written license from ASTRI or an authorized
 *   sublicensor.
 *
 * File Name:
 *   XXX.java
 *
 * Purpose:
 *   
 *   
 *   
 *
 * Project or Modules Name:
 *   
 *
 * Modules Contained:
 *   N/A
 *
 * Modification History:
 * Date(DDMMYY)  Version No Modified By  Description
 * ------------  ---------- -----------  -----------
 * 20160405       0.0.0.1    XXX     Created
 *
 ******************************************************************************
 */


package com.j.thetabk.thetas;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

import com.j.thetabk.R;


/**
 * Created by user on 19/4/2016.
 */
public class ThetaSPropertyB extends ThetaSPropertyA {


    public DialogInterface.OnMultiChoiceClickListener multiChoiceListOnClickListener = new DialogInterface.OnMultiChoiceClickListener(){

        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            selection[which]=isChecked;
        }
    };

    public ThetaSPropertyB() {
        super();
    }

    public void PopList(Context ctx, int index)
    {
        if(selection!=null)
        {
            if(supportDisplay.length>1)
                new AlertDialog.Builder(ctx, AlertDialog.THEME_DEVICE_DEFAULT_DARK)
                        .setTitle(propertyName[index])
                        .setMultiChoiceItems(supportDisplay, selection, multiChoiceListOnClickListener)
                        .setPositiveButton(R.string.ok, multiChoiceListOnClickYesListener)
                        .show();
            return;
        }
        if(supportDisplay.length>1)
            new AlertDialog.Builder(ctx, AlertDialog.THEME_DEVICE_DEFAULT_DARK)
                    .setTitle(propertyName[index])
                    .setItems(supportDisplay, singleChoiceListOnClickListener)
                    .show();

    }

    public interface PropertyChangeListener {
        void onChoiceChanged();
        void onCheckNumChanged();
    }

    public PropertyChangeListener mListener=null;

    public DialogInterface.OnClickListener singleChoiceListOnClickListener = new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (choice == which)
                return;
           choice = which;
            if(mListener!=null)
                mListener.onChoiceChanged();
        }
    };


    public boolean SyncChoiceFromSelection()
    {
        boolean choiceChange=false;
        if(checkedNum == 0)
        {
            selection[choice]=true;
            checkedNum=1;
            return choiceChange;
        }
        int i=FirstTrue();
        if(choice==i)
        {
            return choiceChange;
        }
        choice = i;
        choiceChange=true;
        return choiceChange;
    }

    public DialogInterface.OnClickListener multiChoiceListOnClickYesListener = new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

            int i=FirstTrue();
            if(i==-1)
                return;
            if(i==-2)
            {
                selection[choice]=true;
                if(checkedNum!=1)
                {
                    checkedNum=1;
                    if(mListener!=null)
                        mListener.onCheckNumChanged();
                }
                return;
            }

            int cn=checkedNum;
            TrueCount();

            if(choice==i)
            {
                if(cn!=checkedNum)
                {
                    if(mListener!=null)
                        mListener.onCheckNumChanged();
                }
                return;
            }
            choice=i;

            if(mListener!=null)
                mListener.onChoiceChanged();


        }
    };

}
